﻿using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace Notoric.Gui.Controls.Adorners
{
    /// <summary>
    /// Implements an adorner that allows the user to resize, rotate and the adorned element.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The base of this code was taken from the "WPF Diagram Designer: Part 1" article on CodeProject.com written by "sukram" and adapted.
    /// See <a href="http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part1.aspx">http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part1.aspx</a>
    /// </para>
    /// </remarks>
    /// <seealso cref="RRMChrome"/>
    public class RRMAdorner : Adorner
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="RRMAdorner"/> instance.
        /// </summary>
        /// <param name="adornedElement">
        /// <para>
        /// Type: <see cref="UIElement"/>
        /// </para>
        /// <para>
        /// The adorned element.
        /// </para>
        /// </param>
        public RRMAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            SnapsToDevicePixels = true;

            chrome = new RRMChrome();
            chrome.DataContext = adornedElement;

            visuals = new VisualCollection(this);
            visuals.Add(this.chrome);
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when the user has finished moving the adorned element.
        /// </summary>
        public event EventHandler<MoveCompletedEventArgs> MoveCompleted
        {
            add { chrome.MoveCompleted += value; }
            remove { chrome.MoveCompleted -= value; }
        }

        /// <summary>
        /// Occurs when the user has finished resizing the adorned element.
        /// </summary>
        public event EventHandler<ResizeCompletedEventArgs> ResizeCompleted
        {
            add { chrome.ResizeCompleted += value; }
            remove { chrome.ResizeCompleted -= value; }
        }

        /// <summary>
        /// Occurs when the user has finished rotating the adorned element.
        /// </summary>
        public event EventHandler<RotateCompletedEventArgs> RotateCompleted
        {
            add { chrome.RotateCompleted += value; }
            remove { chrome.RotateCompleted -= value; }
        }

        #endregion

        #region Overrides

        /// <inheritdoc/>
        protected override int VisualChildrenCount
        {
            get
            {
                return this.visuals.Count;
            }
        }

        /// <inheritdoc/>
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            this.chrome.Arrange(new Rect(arrangeBounds));
            return arrangeBounds;
        }

        /// <inheritdoc/>
        protected override Visual GetVisualChild(int index)
        {
            return this.visuals[index];
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the collection of visual childs.
        /// </summary>
        private VisualCollection visuals;
        /// <summary>
        /// Holds the adorner chrome.
        /// </summary>
        private RRMChrome chrome;

        #endregion
    }
}
